#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2024/8/13 1:16
# @Author   : MingTai
# @File     : sliding_window.py

# 无重复最长子串:给定一个字符串 s ，请你找出其中不含有重复字符的 最长 子串 的长度
def find_max_str(string):
    # left_pivot, right_pivot, length, max_length = 0, 0, 0, 0
    start, res, cache = 0, 0, {}
    for index, value in enumerate(string):
        if value in cache and cache[value] >= start:
            start = cache[value] + 1
            cache[value] = index
        else:
            cache[value] = index
            cur = index - start + 1
            res = max(res, cur)
    return res


# 长度最小的子数组
def min_array(target, nums):
    left_pivot = 0
    right_pivot = 0
    current_sum = 0
    min_length = 0
    while right_pivot < len(nums):
        current_sum = current_sum + nums[right_pivot]
        while current_sum >= target:
            # 满足要求更新最优结果
            if right_pivot - left_pivot + 1 < min_length or min_length == 0:
                min_length = right_pivot - left_pivot + 1
            # 窗口缩小，移除left的元素，left右移
            current_sum = current_sum - nums[left_pivot]
            left_pivot = left_pivot + 1
        right_pivot += 1

        # 返回最优结果
    return min_length


# python中字典dict的用法
def test_dir_str():
    aaa_list = ['ee', 'a', 'b', 'c']
    bbb_dict = {
        'one': 1,
        'two': 2,
        'three': 3
    }
    ccc_dict = {'four': 4, 'five': 5}
    ddd_string = 'asdfghjklmnopqrstuvwxyz'

    # 获取索引必须使用 enumerate() 函数
    for key, value in enumerate(aaa_list):
        print(f'aaa_list_key: {key}, aaa_list_value: {value}')

    for key, value in bbb_dict.items():
        print(f'bbb_dict_key2: {key}, bbb_dict_value2: {value}')

    for aaa_key in bbb_dict:  # 这种情况只能获取对应的key，value需要根据key获取
        print(f'aaa_key={aaa_key}，aaa_key_value: {bbb_dict[aaa_key]}')

    # 字典根据key获取值
    print(bbb_dict['one'])

    # 迭代字符串对象，使用enumerate函数
    for key, value in enumerate(ddd_string):
        print(f'ddd_string_key: {key}, ddd_string_value: {value}')

    #  这种方式只能获取值，如果要获取所以必须要用enumerate()函数，否则会报错
    for i in ddd_string:
        print(f'ddd_string_key_i: {i}')

    # 根据key获取value
    print(ccc_dict['five'])


if __name__ == '__main__':
    # print(find_max_str('abcdefghikkkkkjklmn'))
    # print(min_array(target=7, nums=[2, 3, 1, 2, 4, 3]))
    test_dir_str()
    ll = "abab"
    print(ll[1:len(ll) - 1])  # 左闭右开，左边的索引取值，右边的索引不取值，这个相当于ll[1:3]
